package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysTender;
import com.ruoyi.system.domain.vo.CodeVo;
import com.ruoyi.system.mapper.SysTenderMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysContractMapper;
import com.ruoyi.system.domain.SysContract;
import com.ruoyi.system.service.ISysContractService;

/**
 * 合同Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-27
 */
@Service
public class SysContractServiceImpl implements ISysContractService {
    @Autowired
    private SysContractMapper sysContractMapper;

    @Autowired
    private SysTenderMapper sysTenderMapper;

    /**
     * 查询合同
     *
     * @param contractId 合同主键
     * @return 合同
     */
    @Override
    public SysContract selectSysContractByContractId(Long contractId) {
        return sysContractMapper.selectSysContractByContractId(contractId);
    }

    /**
     * 查询合同列表
     *
     * @param sysContract 合同
     * @return 合同
     */
    @DataScope(deptAlias = "d", userAlias = "u")
    @Override
    public List<SysContract> selectSysContractList(SysContract sysContract) {

        List<SysContract> contractList = sysContractMapper.selectSysContractList(sysContract);

        for (SysContract contract : contractList) {

            if (contract.getBidBatch() != null) {
                SysTender sysTender = sysTenderMapper.selectSysTenderByTenderId(Long.valueOf(contract.getBidBatch()));

                if (sysTender != null) {
                    contract.setBidBatchName(sysTender.getBidBatch());
                }
            }

        }

        return contractList;
    }

    /**
     * 新增合同
     *
     * @param sysContract 合同
     * @return 结果
     */
    @Override
    public int insertSysContract(SysContract sysContract) {
        sysContract.setCreateTime(DateUtils.getNowDate());
        sysContract.setUserId(SecurityUtils.getUserId());
        sysContract.setDeptId(SecurityUtils.getDeptId());
        return sysContractMapper.insertSysContract(sysContract);
    }

    /**
     * 修改合同
     *
     * @param sysContract 合同
     * @return 结果
     */
    @Override
    public int updateSysContract(SysContract sysContract) {
        sysContract.setUpdateTime(DateUtils.getNowDate());
        return sysContractMapper.updateSysContract(sysContract);
    }

    /**
     * 批量删除合同
     *
     * @param contractIds 需要删除的合同主键
     * @return 结果
     */
    @Override
    public int deleteSysContractByContractIds(Long[] contractIds) {
        return sysContractMapper.deleteSysContractByContractIds(contractIds);
    }

    /**
     * 删除合同信息
     *
     * @param contractId 合同主键
     * @return 结果
     */
    @Override
    public int deleteSysContractByContractId(Long contractId) {
        return sysContractMapper.deleteSysContractByContractId(contractId);
    }


    @Override
    public AjaxResult listSelects() {

        List<CodeVo> result = new ArrayList<>();

        List<SysContract> list = sysContractMapper.selectSysContractList(null);

        result = list.stream().map(c -> {

            CodeVo codeVo = new CodeVo();
            codeVo.setLabel(c.getContractName());
            codeVo.setValue(c.getContractId());

            return codeVo;
        }).collect(Collectors.toList());

        return AjaxResult.success(result);
    }
}
